# order_list_page.py
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

class OrderListPage:
    # 初始化 OrderListPage 对象
    def __init__(self, driver):
        self.driver = driver
        self.url = 'http://example.com/orders'
        self.order_list_locator = (By.ID, 'order-list')
        self.order_item_locator = (By.CLASS_NAME, 'order-item')
        self.order_status_locator = (By.CLASS_NAME, 'order-status')
        self.order_date_locator = (By.CLASS_NAME, 'order-date')
        self.order_total_locator = (By.CLASS_NAME, 'order-total')
        self.order_details_button_locator = (By.CLASS_NAME, 'order-details-button')
        self.empty_order_message_locator = (By.ID, 'empty-order-message')

    # 导航至订单列表页面
    def navigate(self):
        self.driver.get(self.url)

    # 获取订单列表
    def get_order_list(self):
        return self.driver.find_elements(*self.order_item_locator)

    # 获取特定订单的状态
    def get_order_status(self, order_index):
        return self.driver.find_elements(*self.order_status_locator)[order_index].text

    # 获取特定订单的日期
    def get_order_date(self, order_index):
        return self.driver.find_elements(*self.order_date_locator)[order_index].text

    # 获取特定订单的总价
    def get_order_total(self, order_index):
        return self.driver.find_elements(*self.order_total_locator)[order_index].text

    # 点击查看订单详情
    def click_order_details(self, order_index):
        details_button = self.driver.find_elements(*self.order_details_button_locator)[order_index]
        details_button.click()

    # 检查订单列表是否为空
    def is_order_list_empty(self):
        return bool(self.driver.find_element(*self.empty_order_message_locator))

    # 等待订单列表加载完成
    def wait_for_order_list_to_load(self):
        WebDriverWait(self.driver, 10).until(
            EC.presence_of_element_located(self.order_list_locator)
        )